package org.rcy.ruicingmarket.controller;

import org.rcy.framework.api.entity.ResultMessage;
import org.rcy.ruicingmarket.order.OrderAddVo;
import org.rcy.ruicingmarket.order.OrderClient;
import org.rcy.ruicingmarket.order.OrderEditVo;
import org.rcy.ruicingmarket.order.OrderQueryRequest;
import org.rcy.ruicingmarket.order.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.rcy.ruicingmarket.order.OrderShowResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author RuicyWu
 * @Description
 * @date 2022/1/7 10:01
 */

@RestController
@RequestMapping("/order")
public class OrderController implements OrderClient {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderService orderService;

    @PostMapping("/create")
    public ResultMessage createOrder(HttpServletRequest request, @RequestBody OrderAddVo vo) {
       logger.info("订单信息：{}",vo.toString());
        try {
            String uid = (String)request.getHeader("uid");
            if(StringUtils.isEmpty(uid)){
                return ResultMessage.error();
            }
            vo.setUid(Long.parseLong(uid));
            return ResultMessage.success(orderService.add(vo));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @PutMapping("/edit")
    public ResultMessage editOrder(@RequestBody OrderEditVo vo){
        try {
            orderService.edit(vo);
            return ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @PostMapping("/querypage")
    public ResultMessage queryPage(@RequestBody OrderQueryRequest request){
        try {
            return ResultMessage.success(orderService.queryPage(request));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @GetMapping("/getorders")
    public ResultMessage getOrderListByUid(HttpServletRequest request){
        try {
           String uid = (String)request.getHeader("uid");
           if(StringUtils.isEmpty(uid)){
               return ResultMessage.error();
           }
            return ResultMessage.success(orderService.getOrdersByUid(Long.parseLong(uid)));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @GetMapping("/getbyid")
    @Override
    public ResultMessage<OrderShowResponse> getByOrderId(@RequestParam("id") Long id) {
        try {
            return ResultMessage.success(orderService.findById(id));
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

}
